﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace lok3_map_builder
{
    class Box
    {
        public Color BoxColor { get; set; }
        public Color FrameColor { get; set; }
        public int X1 { get; set; }
        public int Y1 { get; set; }
        public int X2 { get; set; }
        public int Y2 { get; set; }
        public int Width { get; set; }
        public bool HasFrame { get; set; }
        public bool HasBox { get; set; }
        
        private GraphicsDevice _gd;
        private SpriteBatch _sb;

        public Box(GraphicsDevice gd, SpriteBatch sb, int x1, int y1, int x2, int y2) : this(gd, sb, x1, y1, x2, y2, 1, Color.White, Color.White) { }
        public Box(GraphicsDevice gd, SpriteBatch sb, int x1, int y1, int x2, int y2, int width, Color boxColor, Color frameColor)
        {
            X1 = x1;
            Y1 = y1;
            X2 = x2;
            Y2 = y2;
            Width = width;
            BoxColor = boxColor;
            FrameColor = frameColor;
            HasFrame = true;
            HasBox = true;

            _gd = gd;
            _sb = sb;
        }

        public void Draw() {
            _gd.Clear(Color.CornflowerBlue);
            if (HasBox)
            {
                DrawBox(X1, Y1, X2, Y2);
            }
            if (HasFrame)
            {
                DrawFrame(X1, Y1, X2 + X1, Y2 + Y1, Width);
            }
        }


        private void DrawBox(int x, int y, int x2, int y2)
        {
            Texture2D pixel = new Texture2D(_gd, 1, 1, true, SurfaceFormat.Color);
            pixel.SetData<Color>(new[] { Color.White });
            _sb.Begin();
            _sb.Draw(pixel, new Rectangle(x, y, x2, y2), BoxColor);
            _sb.End();
        }
        private void DrawFrame(int x, int y, int x2, int y2, int width)
        {
            Texture2D pixel = new Texture2D(_gd, 1, 1, true, SurfaceFormat.Color);
            pixel.SetData<Microsoft.Xna.Framework.Color>(new[] { Color.White });
            _sb.Begin();
            _sb.Draw(pixel, new Rectangle(x - width, y - width, x2 - x + (2 * width), width), FrameColor);
            _sb.Draw(pixel, new Rectangle(x2, y, width, y2 - y + width), FrameColor);
            _sb.Draw(pixel, new Rectangle(x2, y2 + width, -(x2 - x), -(width)), FrameColor);
            _sb.Draw(pixel, new Rectangle(x, y2 + width, -(width), -(y2 - y) - width), FrameColor);
            _sb.End();
        }
    }
}
